/*
  作者: 劳兆城
  描述: 地图的工具函数
*/

import coordtransform from 'coordtransform'
import * as Commons from './common'

// 后台返回成功标识
export const ERR_OK = '0000'

// arcgisIcon返回成功标识
export const ARCGISICON_ERR_OK = 1

// 创建 animate
export const createAnimate = (options = {}, callback) => {
  if (!options.Map || !options.Map.getView) {
    Commons._showError('必须传入Map实例')
  }

  options.Map.getView().animate(options, () => {
    if (callback) {
      callback()
    }
  })
}

// 百度转wgs1984
export const baiduChangeWgs = (local) => {
  const bd09togcj02 = coordtransform.bd09togcj02(local[0], local[1])
  const gcj02towgs84 = coordtransform.gcj02towgs84(bd09togcj02[0], bd09togcj02[1])
  return gcj02towgs84
}

// 高德转wgs1984
export const gaodeChangeWgs = (local) => {
  return coordtransform.gcj02towgs84(local[0], local[1])
}

// 用于当地图图层  Zoom 级别 小于 预定className级别时，显示 图标信息 overLayer
let zoomGlobal = 0
export const isShowOverLayer = (MapEl, className) => {
  const zoom = parseInt(MapEl.getView().getZoom())

  if (className) {
    let elArr = Commons._getClassEls(className)
    let str = ''

    if (zoom < +className.split('--')[1]) {
      str = 'none'
    } else {
      str = 'block'
    }

    for (let i = 0; i < elArr.length; i++) {
      elArr[i].style.display = str
    }
  } else {
    if (zoomGlobal === zoom) {
      return
    }
    zoomGlobal = zoom

    let classNames = [
      'GykjMap-isHide--7',
      'GykjMap-isHide--12',
      'GykjMap-isHide--13',
      'GykjMap-isHide--14',
      'GykjMap-isHide--15'
    ]
    classNames.map(item => {
      let elArr = Commons._getClassEls(item)
      let str = ''

      if (zoom < +item.split('--')[1]) {
        str = 'none'
      } else {
        str = 'block'
      }

      for (let i = 0; i < elArr.length; i++) {
        elArr[i].style.display = str
      }
    })
  }
}

// 用于当地图图层缩放移动时，改变arcgisIcon图层
export const changeArcgisIcon = (options = {}) => {
  const { layers } = options
  const needChangeLayer = ['drainPortLayer', 'ironhideHouseLayer', 'barrackLayer', 'otherBuildLayer']

  needChangeLayer.map(layerName => {
    if (
      layers[layerName] &&
      typeof layers[layerName] === 'object' &&
      !Array.isArray(layers[layerName])
    ) {
      layers[layerName].changeSize(layers[layerName].layer)
    }
  })
}

// 用于添加 overlay
export const addOverlayer = (MapWrapper, overlay) => {
  if (!overlay) {
    Commons._showError('addOverlayer方法需要你传入一个overlay或[overlay]')
    return
  }

  if (Array.isArray(overlay)) {
    overlay.map(item => {
      MapWrapper.addOverlay(item)
    })
  } else {
    MapWrapper.addOverlay(overlay)
  }
}

// 用于添加 layer 和 overlay
export const addLayerAndOverlayer = (MapWrapper, res) => {
  const { layer, overLayerArr } = res

  if (layer) {
    MapWrapper.addLayer(layer)
  }

  if (overLayerArr) {
    addOverlayer(MapWrapper, overLayerArr)
  }
}

// 用于删除 overlay
export const removeOverlayer = (MapWrapper, overlay) => {
  if (!overlay) {
    Commons._showError('removeOverlayer方法需要你传入一个overlay或[overlay]')
    return
  }

  if (Array.isArray(overlay)) {
    overlay.map(item => {
      MapWrapper.removeOverlay(item)
    })
  } else {
    MapWrapper.removeOverlay(overlay)
  }
}

// 用于删除 layer 和 overla
export const removeLayerAndOverlayer = (MapWrapper, res) => {
  const { layer, overLayerArr } = res

  if (layer) {
    MapWrapper.removeLayer(layer)
  }

  if (overLayerArr) {
    removeOverlayer(MapWrapper, overLayerArr)
  }
}
